﻿using FirstHomeWork.Model;
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;

namespace FirstHomeWork.Utility
{
    public class SqlCache<T> where T : BaseModel
    {
        static SqlCache()
        {
            Type type = typeof(T);
            if (!type.IsDefined(typeof(TableAttribute), true))
                throw new Exception($"请在实体类上添加特性[Table(表名)]");

            TableAttribute customAttributeDatas = type.GetCustomAttribute<TableAttribute>();
            if (customAttributeDatas != null)
            {

                FindSql = $"select {string.Join(",", type.GetProperties().Select(p => $"[{p.Name}]"))}  from [{customAttributeDatas.Name}] where Id=@Id";

                QuerySql = $"select {string.Join(",", type.GetProperties().Select(p => $"[{p.Name}]"))}  from [{customAttributeDatas.Name}]";

                AddSql = $"Insert into [{customAttributeDatas.Name}] ({string.Join(",", type.GetProperties().Where(p => !p.Name.Equals("Id")).Select(p => $"[{p.Name}]"))}) values ({string.Join(",", type.GetProperties().Where(p => !p.Name.Equals("Id")).Select(p => $"@{p.Name}"))})";

                UpdateSql = $"update  [{customAttributeDatas.Name}]  set {string.Join(",", type.GetProperties().Where(p => !p.Name.Equals("Id")).Select(p => $"{p.Name}=@{p.Name}"))} where Id=@Id";

                DeleteSql = $"delete from [{customAttributeDatas.Name}] where Id=@Id";

            }
        }


        /// <summary>
        /// 添加泛型缓存sql
        /// </summary>
        public static readonly string AddSql = "";

        /// <summary>
        /// 根据编号删除缓存sql
        /// </summary>
        public static readonly string DeleteSql = "";

        /// <summary>
        /// 查询单个泛型缓存sql
        /// </summary>
        public static readonly string FindSql = "";

        /// <summary>
        /// 查询全部集合缓存sql
        /// </summary>
        public static readonly string QuerySql = "";

        /// <summary>
        /// 根据编号修改缓存sql
        /// </summary>
        public static readonly string UpdateSql = "";


    }
}
